/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.systemui.plugin.ui.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.ViewPager;

import com.ecarx.systemui.plugin.AppConstants;
import com.ecarx.systemui.plugin.R;
import com.ecarx.systemui.plugin.controller.BluetoothController;
import com.ecarx.systemui.plugin.ui.adapter.BtWifiAdapter;
import com.ecarx.systemui.plugin.ui.custom.AutoHeightViewPager;
import com.ecarx.systemui.plugin.ui.custom.DialogLayout;
import com.ecarx.systemui.plugin.ui.dialog.base.AutoCloseDialog;
import com.ecarx.systemui.plugin.ui.dialog.base.DialogManager;
import com.ecarx.systemui.plugin.utils.DensityUtils;
import com.ecarx.systemui.plugin.utils.LogUtils;

public class BTWifiDialog extends AutoCloseDialog {

    public static final int DIALOG_TOTAL_WIDTH = DensityUtils.dp2px(530);

    public static final int BT = 1;
    public static final int WIFI = 2;
    private static final String TAG = "BTWifiDialogTag";

    private int[] location;

    private DialogLayout llRoot;
    private AutoHeightViewPager autoHeightVp;
    private RadioGroup rg;

    public BTWifiDialog(@NonNull Context context, int[] location) {
        this(context, 0);
        this.location = location;
    }

    public BTWifiDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initTabTitles();
    }

    private void initTabTitles() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_bt_wifi);
        initView();
        initConfig();
    }


    private void initView() {
        llRoot = findViewById(R.id.ll_root);
        llRoot.setNotifyCallBack(new DialogLayout.NotifyCallBack() {
            @Override
            public void cancelTime() {
                LogUtils.d(TAG, "取消Dialog倒计时关闭");
                cancelTimingClose();
            }

            @Override
            public void startTime() {
                LogUtils.d(TAG, "开始Dialog倒计时打开");
                startTimingClose();
            }
        });
        rg = findViewById(R.id.rg);
        rg.setOnCheckedChangeListener((group, checkedId) -> {
            LogUtils.d(TAG, "onCheckedChanged checkedId:" + checkedId);
            if (R.id.rb_bt == checkedId) {
                autoHeightVp.setCurrentItem(0);
            } else if (R.id.rb_wifi == checkedId) {
                autoHeightVp.setCurrentItem(1);
            } else if(R.id.rb_hotspot == checkedId){
                autoHeightVp.setCurrentItem(2);
            }
        });
        autoHeightVp = findViewById(R.id.auto_height_vp);
        autoHeightVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                autoHeightVp.resetHeight();
                LogUtils.d(TAG, "onPageSelected position:" + position);
                if (position == 0) {
                    rg.check(R.id.rb_bt);
                } else if(position == 1) {
                    rg.check(R.id.rb_wifi);
                } else if(position == 2) {
                    rg.check(R.id.rb_hotspot);
                }
            }
        });
        autoHeightVp.setOffscreenPageLimit(3);
        BtWifiAdapter btWifiAdapter = new BtWifiAdapter(getContext());
        autoHeightVp.setAdapter(btWifiAdapter);
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initConfig() {
        initWindow(location[0]);
    }

    @Override
    protected void delayedDismiss() {
        dismiss();
    }

    @Override
    public void show() {
        BluetoothController.getInstance().resetScanTryCount();
        super.show();
    }

    @Override
    public void dismiss() {
        llRoot.postDelayed(() -> {
            BTWifiDialog.super.dismiss();
            DialogManager.getDialogManager().cleanBTWifiDialog();
        }, 150);
        BluetoothController.getInstance().stopScanMode();
    }

    public void showPage(int btOrWifi) {
        if (autoHeightVp != null) {
            autoHeightVp.post(() -> {
                if (BTWifiDialog.BT == btOrWifi) {
                    rg.check(R.id.rb_bt);
                } else if (BTWifiDialog.WIFI == btOrWifi) {
                    rg.check(R.id.rb_wifi);
                }
            });
        }
    }

    public void showDialogAgain(int btOrWifi) {
        startTimingClose();
        showPage(btOrWifi);
    }

    public void updateConfig(int[] location) {
        this.location = location;
        initConfig();
    }

    @Override
    protected boolean isAutoDismiss() {
        return false;
    }
}
